﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace AutoBuild.Core
{
    public class RunnerSelector : IRunnerSelector
    {
        //---- Members -----------------------------------------------------
        readonly IDictionary<string, IBuildRunner> _runners;

        //---------------------------------------------------------
        public RunnerSelector(IBuildRunner[] runners)
        {
            _runners = new Dictionary<string, IBuildRunner>(runners.ToDictionary(item => item.RunnerName.ToLower(), item => item));
        }

        //---------------------------------------------------------
        public IBuildRunner[] GetAvailableRunners()
        {
            return _runners.Values.ToArray();
        }

        //---------------------------------------------------------
        public IBuildRunner GetRunner(string runnerName)
        {
            if (string.IsNullOrEmpty(runnerName))
            {
                throw new ArgumentException("Runner name cannot be null or empty string!");
            }
            string lowercaseRunnerName = runnerName.ToLower();

            if (_runners.Count == 0 || !_runners.ContainsKey(lowercaseRunnerName))
            {
                return null;
            }
            else
            {
                return _runners[lowercaseRunnerName];
            }
            }
    }
}